﻿using System.Text;
using System.Text.RegularExpressions;

/// <summary>
/// pwd_strength 的摘要说明
/// </summary>
public class Pwd_Strength
{
    public Pwd_Strength()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public static bool Check(string pwd, string uid)
    {
        //密码强度要求：长度不低于8位，至少5个不同字符，包含字母/数字/符号至少两种，密码与用户名不能相似，不包含‘abc’、‘123’、‘pwd’等常用弱串

        pwd = pwd.Trim();
        uid = uid.Trim();
        if (pwd.Length < 8)//长度不低于8位
        {
            return false;
        }

        if (pwd.ToUpper().IndexOf(uid.ToUpper()) >= 0)//防止用户名、密码互包含
            return false;
        if (uid.ToUpper().IndexOf(pwd.ToUpper()) >= 0)
            return false;

        int i;
        string curr_char,sbuf,sbuf2;
        sbuf = "";
        for(i=0;i<pwd.Length;i++)
        {
            curr_char = pwd.Substring(i, 1);
            if (sbuf.IndexOf(curr_char)<0)
                sbuf = sbuf + curr_char;
        }
        if (sbuf.Length < 5)//至少5个不同字符，防止密码字符构成过于简单
            return false;

        sbuf2 = "";
        for (i = 0; i < uid.Length; i++)
        {
            curr_char = uid.Substring(i, 1).ToUpper();
            if (sbuf2.IndexOf(curr_char) < 0)
                sbuf2 = sbuf2 + curr_char;
        }
        if (sbuf.Length >= sbuf2.Length)
            i = sbuf2.Length;
        else
            i = sbuf.Length;
        if (sbuf.Substring(0,i) == sbuf2.Substring(0,i)) //防止用户名、密码类似
            return false;

        if (pwd.ToUpper().IndexOf("PASSWORD") >= 0)//防止常用的弱串
            return false;
        if (pwd.ToUpper().IndexOf("PWD") >= 0)
            return false;
        if (pwd.ToUpper().IndexOf("PASS") >= 0)
            return false;
        if (pwd.IndexOf("123") >= 0)
            return false;
        if (pwd.ToUpper().IndexOf("ABC") >= 0)
            return false;

        //Regex r0 = new Regex("(.)\\1{2}");//不允许连续三个相同字符
        //if (r0.IsMatch(pwd))
        //    return false;
            
        Regex r1 = new Regex("[a-z]");//包含小写字母
        Regex r2 = new Regex(".[A-Z]");//首字符外包含大写字母
        Regex r3 = new Regex("[0-9]");//包含数字
        Regex r4 = new Regex("[^A-Za-z0-9]");//包含非以上字符，即符号
        int level = 0;
        if (r1.IsMatch(pwd))
            level++;
        if (r2.IsMatch(pwd))
            level++;
        if (r3.IsMatch(pwd))
            level++;
        if (level < 3 && r4.IsMatch(pwd))
            level++;
        if (level < 3)
        {
            //lblError.Text = "Password is too weak!";
            return false;
        }
        else
            return true;
    }

    public static void GetSalt(int length,ref string salt)
    {
        //RNGCryptoServiceProvider rngCsp = new RNGCryptoServiceProvider();
        System.Random rd = new System.Random((int)System.DateTime.Now.Ticks);
        string CodeBuf = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!@#$%^&*()_+-={}[]|\\;\":'~`/<>,.";
        int CodeBufLen = CodeBuf.Length;
        StringBuilder strResult = new StringBuilder();
        for (int i = 0; i < length; i++)
        {
            char c = CodeBuf[rd.Next(CodeBufLen)];  // 随机获取字符
            strResult.Append(c.ToString());
        }
        salt = strResult.ToString();
    }
}
